package day4;

import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicInteger;

//通过可周期定长线程池:模拟燃放烟花的过程
//首先点燃引线需要5S  之后每2S 发射一个烟花
//一共有12个烟花   烟花发射完毕 打印新年快乐
public class Demo5{

    //static volatile int count=12;  //控制烟花的数量
    //线程安全的 计数器
    static AtomicInteger count=new AtomicInteger(12);
    public static void main(String[] args) {
        ScheduledExecutorService ses=
                Executors.newScheduledThreadPool(1);
        System.out.println("点燃了烟花的引线!!!");
        ses.scheduleAtFixedRate(()->{
          if(count.get()==12) System.out.println("开始发射烟花了!!!");
          System.out.println("正在发射第"+(13-count.get())+"枚烟花");
          //count--; count.getAndDecrement();  count++; count.getAndIncrement();
           count.getAndDecrement();
        },5,2, TimeUnit.SECONDS);

        while(count.get()!=0){}  //监听烟花是否是最后一枚
        System.out.println("烟花燃放完毕,新年快乐!!!");
        ses.shutdown();
    }
}
